function createPopTag() {
	showPopTagDialog.showdivWidth = 780;
	showPopTagDialog.showdivHeight = 800;
	openDialog(showPopTagDialog, URLParams['popBasePath'] + 'pop/createPopTag.htm',
			function() {
				getAllPopedomIsUse(showPopTagDialog)
			});
}
var showPopTagDialog = new Dialog({
			showdiv : "showPopTagDialog",
			title : "生成权限标签信息"
		});
rootDialog.childDialog = showPopTagDialog;
var updatePopDialog = new Dialog({
			showdiv : "updatePopDialog",
			title : "修改权限信息"
		});
rootDialog.childDialog = updatePopDialog;
var createPopDialog = new Dialog({
			showdiv : "createPopDialog",
			title : "创建权限信息"
		});
rootDialog.childDialog = createPopDialog;	
function getAllPopedomIsUse(obj) {
	Popedom.findAllPopedomIsUse(function(data) {
				var popZoneobj = $('poplistZone');
				var trtemp;
				var tdtemp;
				var winobjtemp = $('showPopTagDialog');
				var cellsize = 5;
				
				winobjtemp.style.height = (winobjtemp.clientHeight + data.length
						/ cellsize * 40);
				for (var i = 0; i < data.length; i++) {
					if (i % cellsize == 0) {
						trtemp = document.createElement("tr");
						trtemp = popZoneobj.appendChild(trtemp);
					}
					tdtemp = document.createElement("td");
					tdtemp = trtemp.appendChild(tdtemp);
					tdtemp.appendChild(document.createTextNode(data[i].pname));
					var checkboxtemp = document.createElement("input");
					checkboxtemp.setAttribute('type', 'checkbox');
					checkboxtemp.setAttribute('id', 'pid');
					checkboxtemp.setAttribute('name', 'pid');
					checkboxtemp.setAttribute('title', data[i].ptext);
					checkboxtemp.setAttribute('value', data[i].pid);
					checkboxtemp = tdtemp.appendChild(checkboxtemp);
				}
				for (var i = 0; i < cellsize
						- Math.ceil(data.length % cellsize); i++) {
					tdtemp = document.createElement("td");
					tdtemp = trtemp.appendChild(tdtemp);
					tdtemp.appendChild(document.createTextNode(" "));
				}
			});
}
function createPopTagString() {
	var pops = $getV('pid');
	if(pops){
	Popedom.getPopedomValue(pops.split(";"), function(data) {
		var logic = $getV('logic');
		var isBreak = $getV('isBreak');
		var userSession = c($getV('userSession')) ? $getV('userSession') : "pop";
		var forwardurl = c($getV('forwardurl')) ? $getV('forwardurl') : "nonepop.htm";
		var popstring = '&lt;%@ taglib uri=\"http://www.aide.com/pop\" prefix=\"pop\"%&gt;<br />';
		popstring += "&lt;!--";
		popstring += "读取会话(" + userSession + ")中的权限，";
		popstring += "如果不存在[";
		popstring += data[1];
		if (logic == "or") {
			popstring += "]中的任何一个权限，";
		} else {
			popstring += "]中的所有权限，";
		}
		if (isBreak == "true") {
			popstring += "则跳转到";
			popstring += forwardurl + "页。";
		} else {
			popstring += "则忽略显示此权限签所包括数据。";
		}
		popstring += "--&gt;<br>";
		popstring += '&lt;pop:pop pop=\"' + data[0] + '\" ';

		if (logic == "or") {
			popstring += 'logic=\"' + logic + '\" ';
		}

		if (isBreak) {
			popstring += 'isBreak=\"' + isBreak + '\" ';
		}

		if ($getV('userSession')) {
			popstring += 'userSession=\"' + userSession + '\" ';
		}

		if ($v('forwardurl')) {
			popstring += 'forwardurl=\"' + forwardurl + '\" ';
		}
		popstring += "&gt;<br>&lt;!--这里填写需要设置此权限的代码--&gt;<br>&lt;/pop:pop&gt;";
		$Q('popcode').innerHTML = popstring;
	});
	}else{
		alert("至少选择一个权限!");
	}
}
function getpop() {
	var popzone = $Q('popedom');
	var temptext = "";
	Popedom.getPopedoms($Q('code').value, function(data) {
				for (var i = 0; i < data.length; i++) {
					temptext += (data[i].pname + ";");
				}
				popzone.innerHTML = temptext;
			});
}
/**
 * 读取权限数据
 */
function initPopedomData(pid) {
	if (pid != null && pid != "") {
		Popedom.chakan(pid, function(data) {
					for (var i = 0; i < data.length; i++) {
						if (data[i].tname == 'sign') {
							DWRUtil.setValue(data[i].tname,data[i].tval);
						} else {
							DWRUtil.setValue(data[i].tname,data[i].tvalue);
						}
					}
				});
	}
}
function newPopedom() {
	var pname = trim_value(DWRUtil.getValue('pname'));
	var ptext = trim_value(DWRUtil.getValue('ptext'));
	var sign = DWRUtil.getValue('sign');
	var pid = DWRUtil.getValue('pid');
	var checkpname = false;
	Popedom.checkPName(pname,pid, function(data) {
				if (data == 1) {
					checkpname = true;
					$Q("pnameerror").innerHTML = "正确！";

				} else {
					checkpname = false;
					$Q("pnameerror").innerHTML = "权限名已存在或包含非法字符！";
				}
				if(ptext.length>120){
					alert('描述不可超过120字');
					checkpname = false;
				}
				if (checkpname) {
						if (pid != null && pid != "") {
							Popedom.updatePopedom(pid, pname, ptext, sign,function(data) {
										if (data == 1) {
											send(popedom);
											alert('权限修改成功！');
										} else {
											alert('权限修改失败！');
										}
										closeDialog(updatePopDialog);
									});
						} else {

							Popedom.newPopedom(pname, ptext, sign,function(data) {
										if (data == 1) {
											send(popedom);
											alert('添加权限成功！');
										} else {
											alert('添加权限失败！' + data);
										}
										closeDialog(createPopDialog);
									});

						}
				}
			});

}

var chakancellfuncs = [function(data) {
			return data.title;
		}, function(data) {
			return data.tvalue;
		}];

var cellfuncs = [function(data) {
			return data.pid;
		}, function(data) {
			var href = document.createElement('a');
			href.setAttribute('href', '#');
			href.onclick = function() {
				rootDialog.showdivWidth = 300;
				rootDialog.showdivHeight = 180;
				rootDialog.title = "查看权限信息";
				openDialog(rootDialog, '', function() {
							var chakantable = document.createElement("table");
							chakantable.style.align = "center";
							chakantable.className = "table";
							rootDialog.contentdiv.appendChild(chakantable);
							var chakantbody = document.createElement("tbody");
							chakantbody.setAttribute('id', 'chakantbody');
							chakantable.appendChild(chakantbody);
							Popedom.chakan(data.pid, function(data) {
										DWRUtil.removeAllRows('chakantbody');
										DWRUtil.addRows('chakantbody', data,
												chakancellfuncs, tableclass);
									});
						});
			};
			href.innerHTML = data.pname;
			return href;
		}, function(data) {
			return data.ptext;
		}, function(data) {
			return data.sign == '1' ? '有效' : '无效';
		}, function(data) {
			var href = document.createElement('a');
			href.setAttribute('href', '#');
			href.onclick = function() {
				openDialog(updatePopDialog, 'add.htm',
						function() {
							initPopedomData(data.pid)
						});
			};
			href.innerHTML = '修改';
			return href;
		}, function(data) {
			var href = document.createElement('a');
			href.setAttribute('href', '#');
			href.onclick = function() {
				if (confirm('确认删除权限(' + data.pname + ')吗?')) {
					Popedom.removePopedomById(data.pid, function(data) {
								if (data == 1) {
									send(popedom);
									alert("删除成功！")
								} else {
									alert("删除失败！")
								}
							});
				};
			};
			href.innerHTML = '删除';
			return href;
		}];
var popedom = {
	navigationsize : 10,
	paramname : 'popedom',
	page : 1,
	funcname : 'Popedom.findAllPopedomPage()',
	cellfuncs : cellfuncs,
	pagesize : 10,
	count : 'Popedom.getCount()',
	idname : 'popedomlistZone'
};

var pageuuid = popedom.paramname;
window.onload = function() {
	initDWR();
	useLoading('../../expimages/blue-loading.gif', 'img');
	Popedom.isAdmin(function(data){
		if(data){
			send(popedom);
		}else{
			alert("请勿非法访问，仅超级管理员可以访问！")
			top.window.location=URLParams['basePath'];
		}
	});
	
};
